﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;

namespace Human
{
	/// <summary>
	/// Summary description for frmUser.
	/// </summary>
	public class frmUser : System.Windows.Forms.Form
	{
		
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox txt_hoten;
		private System.Windows.Forms.TextBox userid;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox password;
        private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Button butOk;
		private System.Windows.Forms.Button butCancel;
		private AccessData m;
        Language lan = new Language();
		private DataSet	ds=new DataSet();
		private DataSet dsr=new DataSet();
		private DataTable dt=new DataTable();
		private DataTable dtdt=new DataTable();
		private DataTable dtnk=new DataTable();
		private DataTable dtcls=new DataTable();
        private DataTable dtmay = new DataTable();
		private DataTable dtbs=new DataTable();
        DataSet ds_nv = new DataSet();
		private string v_user="",v_psw="",v_right="",s_makp="",user="";
        bool m_admin = false;        
        private CheckBox chkAdmin;
        private ComboBox cboBophan;
        private Label label4;
        private TextBox txt_manv;
        private LibList.List lst_hoten;
		private System.ComponentModel.Container components = null;

		public frmUser(AccessData acc,DataSet dset,string user,string psw,string right)
		{
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
			m=acc;	dsr=dset;
			v_user=user;v_psw=psw;v_right=right;
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmUser));
            this.label1 = new System.Windows.Forms.Label();
            this.txt_hoten = new System.Windows.Forms.TextBox();
            this.userid = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.password = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.butOk = new System.Windows.Forms.Button();
            this.butCancel = new System.Windows.Forms.Button();
            this.chkAdmin = new System.Windows.Forms.CheckBox();
            this.cboBophan = new System.Windows.Forms.ComboBox();
            this.label4 = new System.Windows.Forms.Label();
            this.txt_manv = new System.Windows.Forms.TextBox();
            this.lst_hoten = new LibList.List();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.ForeColor = System.Drawing.Color.DarkBlue;
            this.label1.Location = new System.Drawing.Point(1, 12);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(58, 23);
            this.label1.TabIndex = 10;
            this.label1.Text = "Họ tên:";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txt_hoten
            // 
            this.txt_hoten.BackColor = System.Drawing.SystemColors.HighlightText;
            this.txt_hoten.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txt_hoten.Location = new System.Drawing.Point(104, 12);
            this.txt_hoten.MaxLength = 50;
            this.txt_hoten.Name = "txt_hoten";
            this.txt_hoten.Size = new System.Drawing.Size(199, 23);
            this.txt_hoten.TabIndex = 1;
            this.txt_hoten.Validated += new System.EventHandler(this.hoten_Validated);
            this.txt_hoten.TextChanged += new System.EventHandler(this.hoten_TextChanged);
            this.txt_hoten.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_hoten_KeyDown);
            // 
            // userid
            // 
            this.userid.BackColor = System.Drawing.SystemColors.HighlightText;
            this.userid.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.userid.Location = new System.Drawing.Point(59, 39);
            this.userid.MaxLength = 10;
            this.userid.Name = "userid";
            this.userid.Size = new System.Drawing.Size(244, 23);
            this.userid.TabIndex = 3;
            this.userid.Validated += new System.EventHandler(this.userid_Validated);
            this.userid.KeyDown += new System.Windows.Forms.KeyEventHandler(this.userid_KeyDown);
            // 
            // label2
            // 
            this.label2.ForeColor = System.Drawing.Color.DarkBlue;
            this.label2.Location = new System.Drawing.Point(1, 37);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(58, 23);
            this.label2.TabIndex = 12;
            this.label2.Text = "Tên Login:";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // password
            // 
            this.password.BackColor = System.Drawing.SystemColors.HighlightText;
            this.password.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.password.Location = new System.Drawing.Point(59, 65);
            this.password.MaxLength = 10;
            this.password.Name = "password";
            this.password.PasswordChar = '*';
            this.password.Size = new System.Drawing.Size(244, 23);
            this.password.TabIndex = 5;
            this.password.KeyDown += new System.Windows.Forms.KeyEventHandler(this.password_KeyDown);
            // 
            // label3
            // 
            this.label3.ForeColor = System.Drawing.Color.DarkBlue;
            this.label3.Location = new System.Drawing.Point(-2, 62);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(61, 26);
            this.label3.TabIndex = 14;
            this.label3.Text = "Password:";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // butOk
            // 
            this.butOk.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.butOk.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butOk.ForeColor = System.Drawing.Color.DarkBlue;
            this.butOk.Image = global::Human.Properties.Resources.save;
            this.butOk.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butOk.Location = new System.Drawing.Point(104, 145);
            this.butOk.Name = "butOk";
            this.butOk.Size = new System.Drawing.Size(65, 28);
            this.butOk.TabIndex = 9;
            this.butOk.Text = "   &Lưu";
            this.butOk.Click += new System.EventHandler(this.butOk_Click);
            // 
            // butCancel
            // 
            this.butCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.butCancel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butCancel.ForeColor = System.Drawing.Color.DarkBlue;
            this.butCancel.Image = global::Human.Properties.Resources.close_r;
            this.butCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butCancel.Location = new System.Drawing.Point(172, 145);
            this.butCancel.Name = "butCancel";
            this.butCancel.Size = new System.Drawing.Size(65, 28);
            this.butCancel.TabIndex = 10;
            this.butCancel.Text = "  &Thoát";
            this.butCancel.Click += new System.EventHandler(this.butCancel_Click);
            // 
            // chkAdmin
            // 
            this.chkAdmin.AutoSize = true;
            this.chkAdmin.ForeColor = System.Drawing.Color.DarkBlue;
            this.chkAdmin.Location = new System.Drawing.Point(61, 122);
            this.chkAdmin.Name = "chkAdmin";
            this.chkAdmin.Size = new System.Drawing.Size(55, 17);
            this.chkAdmin.TabIndex = 8;
            this.chkAdmin.Text = "Admin";
            this.chkAdmin.UseVisualStyleBackColor = true;
            this.chkAdmin.CheckedChanged += new System.EventHandler(this.chkAdmin_CheckedChanged);
            this.chkAdmin.KeyDown += new System.Windows.Forms.KeyEventHandler(this.chkAdmin_KeyDown);
            // 
            // cboBophan
            // 
            this.cboBophan.FormattingEnabled = true;
            this.cboBophan.Location = new System.Drawing.Point(59, 92);
            this.cboBophan.Name = "cboBophan";
            this.cboBophan.Size = new System.Drawing.Size(244, 21);
            this.cboBophan.TabIndex = 7;
            this.cboBophan.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboBophan_KeyDown);
            // 
            // label4
            // 
            this.label4.ForeColor = System.Drawing.Color.DarkBlue;
            this.label4.Location = new System.Drawing.Point(-2, 89);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(60, 23);
            this.label4.TabIndex = 16;
            this.label4.Text = "Bộ phận:";
            this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // txt_manv
            // 
            this.txt_manv.BackColor = System.Drawing.SystemColors.HighlightText;
            this.txt_manv.Enabled = false;
            this.txt_manv.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txt_manv.Location = new System.Drawing.Point(59, 12);
            this.txt_manv.MaxLength = 50;
            this.txt_manv.Name = "txt_manv";
            this.txt_manv.Size = new System.Drawing.Size(45, 23);
            this.txt_manv.TabIndex = 0;
            this.txt_manv.Validated += new System.EventHandler(this.txt_manv_Validated);
            this.txt_manv.KeyDown += new System.Windows.Forms.KeyEventHandler(this.hoten_KeyDown);
            // 
            // lst_hoten
            // 
            this.lst_hoten.ColumnCount = 0;
            this.lst_hoten.FormattingEnabled = true;
            this.lst_hoten.Location = new System.Drawing.Point(12, 157);
            this.lst_hoten.MatchBufferTimeOut = 1000;
            this.lst_hoten.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
            this.lst_hoten.Name = "lst_hoten";
            this.lst_hoten.Size = new System.Drawing.Size(58, 17);
            this.lst_hoten.TabIndex = 11;
            this.lst_hoten.TextIndex = -1;
            this.lst_hoten.TextMember = null;
            this.lst_hoten.ValueIndex = -1;
            this.lst_hoten.Visible = false;
            this.lst_hoten.KeyDown += new System.Windows.Forms.KeyEventHandler(this.lst_hoten_KeyDown);
            // 
            // frmUser
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(306, 190);
            this.Controls.Add(this.lst_hoten);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.cboBophan);
            this.Controls.Add(this.chkAdmin);
            this.Controls.Add(this.butCancel);
            this.Controls.Add(this.butOk);
            this.Controls.Add(this.password);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.userid);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.txt_manv);
            this.Controls.Add(this.txt_hoten);
            this.Controls.Add(this.label1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmUser";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Thêm người dùng";
            this.Load += new System.EventHandler(this.frmUser_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion
        private void frmUser_Load(object sender, System.EventArgs e)
        {
            string sql = "";
            user = m.user;
            load_bophan();
            load_lstnhanvien();
            if (v_user != "")
            {
                this.Text = lan.Change_language_MessageText("Sửa Password");
                foreach (DataRow r in m.get_data("select * from " + m.user + ".pwhuman where username='" + v_user + "'").Tables[0].Rows)
                {
                    txt_hoten.Text = r["ten"].ToString();
                    v_psw = m.decode(r["pass"].ToString().Trim());
                    s_makp = r["makp"].ToString();
                    m_admin = (bool)(r["_admin"]);
                    break;
                }
                if (txt_hoten.Text == "") txt_hoten.Text = v_user;
                userid.Text = v_user;
                password.Text = v_psw;
                cboBophan.SelectedValue = s_makp;
                chkAdmin.Checked = m_admin;
                userid.Enabled = false;
            }
            txt_hoten.Focus();
        }
        private void load_lstnhanvien()
        {
            string sql = " select l.manv,trim(l.ho)||' '||trim(l.ten) as hoten ";
            sql += " from "+user+".llnv l inner join "+user+".hopdong hd on l.manv=hd.manv";
            sql += " inner join "+user+".max_lan ml on hd.manv=ml.manv";
            sql += " where hd.lan=ml.lan and hd.ngaynghi is null";
            ds_nv = m.get_data(sql);
            lst_hoten.DisplayMember = "manv";
            lst_hoten.ValueMember = "hoten";
            lst_hoten.DataSource = ds_nv.Tables[0];
        }
        private void load_bophan()
        {
            cboBophan.DisplayMember = "tenbp";
            cboBophan.ValueMember = "madv";
            cboBophan.DataSource = m.get_bophan().Tables[0];
        }
		private void butOk_Click(object sender, System.EventArgs e)
		{			
            if (txt_hoten.Text=="")
			{
				MessageBox.Show(lan.Change_language_MessageText("Yều cầu nhập họ tên!"),AccessData.Msg); 
				txt_hoten.Focus();
				return;
			}
			if (userid.Text=="")
			{
				MessageBox.Show(lan.Change_language_MessageText("Yều cầu nhập tên login!"),AccessData.Msg);
				userid.Focus();
				return;
			}
            if (cboBophan.SelectedIndex != -1)
                s_makp = cboBophan.SelectedValue.ToString();
            else
                s_makp = "";

            m.upd_pwhuman(userid.Text.ToUpper().Trim(), txt_hoten.Text, m.encode(password.Text.ToUpper().Trim()), v_right,s_makp,chkAdmin.Checked);
            m.updrec(dsr.Tables[0], userid.Text.ToUpper().Trim(), m.encode(password.Text.ToUpper().Trim()), v_right);
			this.Close();
		}

		private void butCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private bool test()
		{
			if (txt_hoten.Text=="")
			{
				MessageBox.Show(lan.Change_language_MessageText("Yều cầu nhập họ tên!"),AccessData.Msg);
				txt_hoten.Focus();
				return false;
			}

			if (userid.Text=="")
			{
				MessageBox.Show(lan.Change_language_MessageText("Yều cầu nhập tên login!"),AccessData.Msg);
				userid.Focus();
				return false;
			}

			return true;
		}
		

		private void hoten_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void userid_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void password_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}{F4}");
		}

		private void userid_Validated(object sender, System.EventArgs e)
		{
			if (userid.Text!="")
			{
				DataRow r=m.getrowbyid(dsr.Tables[0],"username='"+userid.Text+"'");
				if (r!=null)
				{
					MessageBox.Show(lan.Change_language_MessageText("Tên login đã có!"),AccessData.Msg);
					userid.Focus();
					return;
				}
			}
		}
		public DataSet dsright
		{
			get
			{
				return dsr;
			}
		}	

		private void mabs_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

        private void hoten_Validated(object sender, EventArgs e)
        {
            if (txt_hoten.Text!="")
                txt_hoten.Text = m.Caps(txt_hoten.Text);
        }

        private void chkAdmin_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAdmin.Checked)
            {
                cboBophan.SelectedIndex = -1;
            }
            else
            {
                cboBophan.SelectedValue = s_makp;
            }
        }

        private void cboBophan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void chkAdmin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                SendKeys.Send("{Tab}");
        }

        private void hoten_TextChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == this.txt_hoten)
            {
                this.loc_hoten(txt_hoten.Text.Trim());
                this.lst_hoten.BrowseToDanhmuc(txt_hoten, txt_manv,userid,40);
            }
        }
        private void loc_hoten(string hoten)
        {
            try
            {
                CurrencyManager cm = (CurrencyManager)this.BindingContext[lst_hoten.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = "hoten like '%" + hoten.Trim() + "%' ";
            }
            catch
            {

            }
        }
        private void txt_hoten_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.Down) || (e.KeyCode == Keys.Up))
            {
                this.lst_hoten.Focus();
            }
            else if (e.KeyCode == Keys.Return)
            {
                if (this.lst_hoten.Visible)
                {
                    this.lst_hoten.Focus();
                }
                else
                {
                    SendKeys.Send("{Tab}");
                }
            }
        }
        DataRow[] dr_nv;
        private void txt_manv_Validated(object sender, EventArgs e)
        {
            if (txt_manv.Text.Trim() != "")
            {
                this.dr_nv = ds_nv.Tables[0].Select("manv='" + txt_manv.Text.Trim() + "'");
                if (dr_nv != null)
                {
                    txt_hoten.Text = dr_nv[0]["hoten"].ToString().Trim();
                }
                else
                {
                    txt_hoten.Text = "";
                }
            }
        }

        private void lst_hoten_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                txt_manv_Validated(null, null);
            }
        }
	}
}
